﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Military_Madness
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            ++Offset;
            using (var Graphics = this.CreateGraphics())
            {
                DrawMap(Graphics);
            }
            base.OnMouseDown(e);
        }
        int Offset = 0;

        protected override void OnSizeChanged(EventArgs e)
        {
            using (var Graphics = this.CreateGraphics())
            {
                DrawMap(Graphics);
            }
            base.OnSizeChanged(e);
        }
        public void DrawMap(Graphics Graphics)
        {
            var File = new FileInfo(@"C:\Users\ChenXW\Desktop\fengkuangjundui_1000\res\raw\map01.NMD");

            // Graphics.Clear(Color.White);

            using (var Stream = File.OpenRead())
            {
                byte[] Bytes = new byte[2];
                int ReadCount;

                int X = 0;
                int Y = 0;

                while ((ReadCount = Stream.Read(Bytes, 0, Bytes.Length)) > 0)
                {
                    int Index = (Bytes[0] + (Bytes[1] << 8)) + Offset;

                    if (Index >= 260)
                    {
                        X = 0;
                        Y = 0;

                        continue;
                    }

                    Map.DrawChip(Graphics, Index, Y, X);

                    if (X + 48 * 2 > Width)
                    {
                        X = 0;
                        Y += 48;
                    }
                    else
                    {
                        X += 48;
                    }
                }

            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            DrawMap(e.Graphics);
            base.OnPaint(e);
        }
    }
}